﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dqs_DAL
{
	public class Student_DAL
	{
		/// <summary>
		/// 查询所有学生信息
		/// </summary>
		/// <returns></returns>
		public List<StudentInfo> QueryStudentInfo()
		{
			using (MyDbContext db = new MyDbContext())
			{
				var students = db.Students.Where(s=>s.DelFlag == 0).ToList();
				return students;
			}
		}

		public StudentInfo IsLoginOfAdmin(string name, string pwd)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var result = db.Students.Where(a => a.UserName == name).Where(a => a.Password == pwd).FirstOrDefault();
				if (result != null)
				{
					return result;
				}

				return null!;
			}
		}

		#region 学生部分
		/// <summary>
		/// 插入学生信息
		/// </summary>
		/// <param name="student"></param>
		public bool InsertStudentInfo(StudentInfo students)
		{
			using (MyDbContext db = new MyDbContext())
			{
				if (students == null)
					return false;
				db.Students.Add(students);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过学生Id修改学生信息
		/// </summary>
		/// <param name="student"></param>
		public bool UpdateStudentInfoById(StudentInfo studentNew)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var studentOld = db.Students.Where(x => x.Id == studentNew.Id).FirstOrDefault();
				if (studentOld == null)
				{
					return false;
				}
				studentOld.StuNum = studentNew.StuNum;
				studentOld.UserName = studentNew.UserName;
				studentOld.Password = studentNew.Password;
				studentOld.CourseList = studentNew.CourseList;
				studentOld.QuestionCount = studentNew.QuestionCount;
				studentOld.AttendCount = studentNew.AttendCount;
				studentOld.Phone = studentNew.Phone;
				studentOld.FullName = studentNew.FullName;
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过学生Id删除学生信息
		/// </summary>
		/// <param name="studentId"></param>
		/// <returns></returns>
		public bool DeleteStudentInfoById(int studentId)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var student = db.Students.FirstOrDefault(s => s.Id == studentId);
				if (student == null)
				{
					return false;
				}
				//db.Students.Remove(student);
				student.DelFlag = 1;
				db.SaveChanges();
			}
			return true;
		}


		
		#endregion
	}
}
